﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.DDD.Web.Default.users
{
    public class showAction  : Controller
	{
		public showAction(System.IO.TextWriter tw):base(tw){}
		public showAction(string fileName) : base(fileName) {}
		#if (!AspNET && !MicrosoftOwin && !NFinalOwin)
		public showAction(HttpContext context):base(context){}
		public showAction(Microsoft.Owin.IOwinContext context):base(context){}
		public showAction(NFinal.Owin.HtmlWriter writer,NFinal.Owin.Request request):base(writer,request){}
		#endif
		#if AspNET
		public showAction(HttpContext context):base(context){}
		#endif
		#if MicrosoftOwin
		public showAction(Microsoft.Owin.IOwinContext context):base(context){}
		#endif
		#if NFinalOwin
		public showAction(NFinal.Owin.HtmlWriter writer,NFinal.Owin.Request request):base(writer,request){}
		#endif
		public showAction(Controller controller):base(controller){}
        public class __show_us__:NFinal.Struct
		{
			/// <summary>
            /// 用户表ID
            /// </summary>
			public System.Int64 id;
			/// <summary>
            /// 名称
            /// </summary>
			public System.String name;
			/// <summary>
            /// 密码
            /// </summary>
			public System.String pwd;
			#region 写Json字符串
			public override void WriteJson(System.IO.TextWriter tw)
			{
				tw.Write("{");
						tw.Write("\"id\":");
						tw.Write(id.ToString());
						tw.Write(",");
						tw.Write("\"name\":");
						if(name==null)
						{
							tw.Write("null");
						}
						else
						{
							tw.Write("\"");
							tw.Write(name);
							tw.Write("\"");
						}
						tw.Write(",");
						tw.Write("\"pwd\":");
						if(pwd==null)
						{
							tw.Write("null");
						}
						else
						{
							tw.Write("\"");
							tw.Write(pwd);
							tw.Write("\"");
						}
				tw.Write("}");
			}
			#endregion
		}
		public void show(int p)
        {
            var con = new System.Data.SQLite.SQLiteConnection(NFinal.Config.ConfigurationManager.ConnectionStrings["Common"].ConnectionString);
			con.Open();
            //创建Page对象
            NFinal.Page pg = new NFinal.Page(p, 5);
            //计算URL所用到的函数
            pg.GetUrlFunction = (pi) => { return Url.DDD_users_show(pi); };
            #region	var us;分页
			var __show_us_command__ = new System.Data.SQLite.SQLiteCommand("select count(*) from users", con);
			pg.recordCount =System.Convert.ToInt32(__show_us_command__.ExecuteScalar());
			__show_us_command__.Dispose();
			pg.count = (pg.recordCount % pg.size==0)? pg.recordCount/pg.size:pg.recordCount/pg.size+1;
			//传页码时用的变量名
			if (pg.index > pg.count)
            {
                pg.index = pg.count;
            }
            if (pg.index < 1)
            {
                pg.index = 1;
            }
			var us = new NFinal.List<__show_us__>();
            //计算得到SQL语句
			__show_us_command__ = new System.Data.SQLite.SQLiteCommand(string.Format("select * from users Limit {0} Offset {1}",pg.size,(pg.index-1)*pg.size), con);
			var __show_us_reader__= __show_us_command__.ExecuteReader();
			if (__show_us_reader__.HasRows)
			{
				while (__show_us_reader__.Read())
				{
					var __show_us_temp__ = new __show_us__();
					__show_us_temp__.id = __show_us_reader__.GetInt64(0);
					__show_us_temp__.name =__show_us_reader__.IsDBNull(1)?null: __show_us_reader__.GetString(1);
					__show_us_temp__.pwd =__show_us_reader__.IsDBNull(2)?null: __show_us_reader__.GetString(2);
					us.Add(__show_us_temp__);
				}
			}
			__show_us_reader__.Dispose();
			__show_us_command__.Dispose();
			#endregion
			
            con.Close();
            
			Write("<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head runat=\"server\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title></title><script src=\"/Scripts/jquery-1.11.2.min.js\"></script><script src=\"/Scripts/Url.js\"></script></head><body><form id=\"form1\" runat=\"server\"><div><table>");
			var u= us.GetEnumerator(); while(u.MoveNext()){
				Write("<tr><td><input id=\"id_");
				Write(u.Current.id);
				Write("\" type=\"text\" value=\"");
				Write(u.Current.id);
				Write("\" readonly=\"readonly\" /></td><td><input id=\"name_");
				Write(u.Current.id);
				Write("\" type=\"text\" value=\"");
				Write(u.Current.name);
				Write("\" /></td><td><input id=\"pwd_");
				Write(u.Current.id);
				Write("\" type=\"text\" value=\"");
				Write(u.Current.pwd);
				Write("\" /></td><td><button onclick=\"save(");
				Write(u.Current.id);
				Write(");\" type=\"button\">save</button><button onclick=\"del(");
				Write(u.Current.id);
				Write(");\" type=\"button\">delete</button></td></tr>");
			}
			Write("</table>");
			pg.__render__ = ()=>{ 
				Write("﻿当前页:");
				Write(pg.index);
				Write("/");
				Write(pg.count);
				Write("<a href=\"");
				Write(pg.GetUrlFunction(1));
				Write("\">首页</a><a href=\"");
				Write(pg.index <= 1 ? "#" : pg.GetUrlFunction(pg.index - 1));
				Write("\">上一页</a>");
				for(int i = ((pg.index - 1) / pg.navigatorSize) * pg.navigatorSize + 1;i <= pg.count && i <= ((pg.index - 1) / pg.navigatorSize + 1) * pg.navigatorSize;i++){
					if(i == pg.index){
						Write("<a style=\"color:red;\" href=\"");
						Write(pg.GetUrlFunction(i));
						Write("\">");
						Write(i);
						Write("</a>");
					}else{
						Write("<a href=\"");
						Write(pg.GetUrlFunction(i));
						Write("\">");
						Write(i);
						Write("</a>");
					}
				}
				Write("<a href=\"");
				Write(pg.index >= pg.count ? "#" : pg.GetUrlFunction(pg.index + 1));
				Write("\">下一页</a><a href=\"");
				Write(pg.GetUrlFunction(pg.count));
				Write("\">末页</a>");
			};pg.__render__();
			Write("<br /> 插入 <input id=\"name\" type=\"text\" value=\"\" /><input id=\"pwd\" type=\"text\" value=\"\" /><input type=\"button\" onclick=\"insert();\" value=\"insert\" /></div></form></body></html> <script> function del(id) { $.post(Url.DDD_users_delete(id), {}, function (data) { if (data) { alert(\"删除成功\"); } else { alert(\"删除失败\"); } }); } function insert() { var id = $(\"#id\").val(); var name = $(\"#name\").val(); var pwd = $(\"#pwd\").val(); $.post(Url.DDD_users_insert(id, name, pwd), {}, function (data) { if (data) { alert(\"添加成功\"); } else { alert(\"添加失败\"); } }); } function save(ext) { var id = $(\"#id_\" + ext).val(); var name = $(\"#name_\" + ext).val(); var pwd = $(\"#pwd_\" + ext).val(); $.post(Url.DDD_users_edit(id, name, pwd), {}, function (data) { if (data) { alert(\"保存成功\"); } else { alert(\"保存失败\"); } }); } </script>");
        }
        
        
        
    }
}